﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using TianShiProject.EntityFrameworkCore;
using TianShiProject.Models.CarService.Accidents;
using TianShiProject.Models.Common;
using Volo.Abp.EntityFrameworkCore;

namespace TianShiProject.CarService.Accidents
{
    public class AccidentRepository : DapperDbContextRepository, IAccidentRepository
    {
        public AccidentRepository(IDbContextProvider<TianShiProjectDbContext> dbContextProvider) : base(dbContextProvider)
        {

        }
        /// <summary>
        /// 显示事故记录
        /// </summary>
        /// <param name="name"></param>
        /// <param name="time"></param>
        /// <param name="cardnumber"></param>
        /// <param name="pageindex"></param>
        /// <param name="pagesize"></param>
        /// <returns></returns>
        public async Task<PageResponse<AccidentDto>> GetShowAccident(string name, DateTime? time, string cardnumber, int pageindex, int pagesize)
        {
            PageResponse<AccidentDto> response = new PageResponse<AccidentDto>();
            var sql = $"select a.Id,a.AccidentlNumber,a.Title,a.CarNumber,b.License,a.AccidentTime,a.Remark,a.CreateTime,a.IsDelete,a.AccidentDesc,a.AccidentResult,a.Compensate,a.Loss,b.DriverName  as AccidentManager from Accident a join Car b on a.CarNumber=b.CarNumber where a.IsDelete=0 ";

            if (!string.IsNullOrWhiteSpace(name))
            {
                sql += $" and a.Title like '%" + name + "%'";
            }
            if (time != null)
            {
                sql += $" and a.AccidentTime='{time}'";
            }
            if (!string.IsNullOrWhiteSpace(cardnumber))
            {
                sql += $" and a.CarNumber='{cardnumber}'";
            }
            var list = await this.QueryAsync<AccidentDto>(sql, null);
            response.PageTotal = list.Count;
            sql += $"order by a.Id offset {(pageindex - 1) * pagesize} rows fetch next {pagesize} rows only";
            var lists = await this.QueryAsync<AccidentDto>(sql, null);
            response.PageData = lists;
            return response;
        }
        /// <summary>
        /// 删除事故记录
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<int> DeleteDelAccident(int id)
        {
            var sql = $"update Accident set IsDelete=1 where Id={id}";
            return await ExecuteAsync(sql, null);
        }
        /// <summary>
        /// 批量删除事故记录
        /// </summary>
        /// <returns></returns>
        public async Task<int> DeleteDelsAccident(string ids)
        {
            var list = 0;
            var id = ids.Split(',');
            foreach (var item in id)
            {
                var sql = $"update Accident set IsDelete=1 where Id={item}";
                list = await ExecuteAsync(sql, null);
            }
            return list;
        }
        /// <summary>
        /// 编辑修改
        /// </summary>
        /// <param name="accidentDto"></param>
        /// <returns></returns>
        public async Task<int> PutUpdateAccident(AccidentDto accidentDto)
        {
            var sql = $"update Accident set Title='{accidentDto.Title}',CarNumber='{accidentDto.CarNumber}',Remark='{accidentDto.Remark}',AccidentDesc='{accidentDto.AccidentDesc}',AccidentResult='{accidentDto.AccidentResult}',Compensate='{accidentDto.Compensate}',Loss='{accidentDto.Loss}',AccidentTime='{accidentDto.AccidentTime.ToString("yyyy/MM/dd")}' where Id='{accidentDto.Id}'";
            return await ExecuteAsync(sql, null);
        }
        /// <summary>
        /// 添加事故记录
        /// </summary>
        /// <param name="accidentDto"></param>
        /// <returns></returns>
        public async Task<int> PostAddAccident(AccidentDto accidentDto)
        {
            var sql = $"insert into Accident values('{accidentDto.AccidentlNumber}','{accidentDto.Title}','{accidentDto.CarNumber}','','','{accidentDto.AccidentTime.ToString("yyyy/MM/dd")}','{accidentDto.Remark}',GETDATE(),0,'{accidentDto.AccidentDesc}','{accidentDto.AccidentResult}','{accidentDto.Compensate}','{accidentDto.Loss}')";
            var list = await ExecuteAsync
               (sql, accidentDto);
            return list;
        }
    }
}
